package com.nowcoder.community.util;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;

import java.util.Objects;

/**
 * @author bombax
 * @date 2024-07-21 17:49
 */
public class CookieUtil {
    public static String getCookie(HttpServletRequest request, String name) {
        if (Objects.isNull(request) || Objects.isNull(name)) {
            throw new IllegalArgumentException("参数为空");
        }
        jakarta.servlet.http.Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
}
